/**
 * 
 */
package com.qiyou.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.qiyou.dao.IFriendDao;
import com.qiyou.entity.Friend;
import com.qiyou.entity.UserInfo;
import com.qiyou.util.DBUtil;
import com.qiyou.util.TransformUtil;

/**
 * @author 赛艳志
 *
 */
public class FriendDaoImpl implements IFriendDao {
    private Connection conn;
    
    public FriendDaoImpl(Connection conn){
        this.conn = conn;
    }
    
    @Override
    public boolean doCreate(Friend friend) throws SQLException {
        String sql = "insert into tb_friends(userID,friendID,friendDate) values(?,?,?)";
        Object[] objects = TransformUtil.beanTOArray(friend, false);
        return DBUtil.execute(conn, sql,objects);
    }

    @Override
    public boolean doDelete(String userID, String friendID) throws SQLException {
        String sql = "delete from tb_friends where userID=? and friendID=?";
        return DBUtil.execute(conn, sql, userID,friendID);
    }

    @Override
    public List<UserInfo> getFriends(String userID) throws SQLException {
        List<UserInfo> uilist = null;
        String sql = "select tb_user_info.* from tb_user_info,tb_friends " +
        		"where tb_friends.userID=? and tb_user_info.userID=tb_friends.friendID";
        List<Object[]> all = DBUtil.query(conn, sql, userID);
        if (null!=all&&all.size()>0) {
            uilist = new ArrayList<UserInfo>(all.size());
            UserInfo userInfo = null;
            for (Object[] objects : all) {
                userInfo = (UserInfo) TransformUtil.arrayToBean(objects, UserInfo.class);
                uilist.add(userInfo);
            }
        }
        return uilist;
    }

}
